﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace VatGia
{
    public partial class FormQLGianHang : Form
    {
        private FormMain1 frm;
        public FormQLGianHang(FormMain1 frm)
        {
            this.frm = frm;
            InitializeComponent();
        }
        DataService dservice = new DataService();
        DataTable dt = new DataTable();
        public int maGH_select;


        private void dGV_DSGianHang_SelectionChanged(object sender, EventArgs e)
        {
            for (int i = dGV_DSGianHang.SelectedRows.Count - 1; i >= 0; i--)
            {
                if (dGV_DSGianHang.SelectedRows[i].Index <= dGV_DSGianHang.Rows.Count - 1)
                {
                    maGH_select = Int32.Parse(dGV_DSGianHang.SelectedRows[i].Cells[0].Value.ToString());
                   /* txtNoiDung.Text = dGV_DSTinNhan.SelectedRows[i].Cells[1].Value.ToString();
                    cbox_LoaiTinNhan.Text = dGV_DSTinNhan.SelectedRows[i].Cells[2].Value.ToString();
*/
                    txtTenGH.Text = dGV_DSGianHang.SelectedRows[i].Cells[1].Value.ToString();
                    txtDiaChiGH.Text = dGV_DSGianHang.SelectedRows[i].Cells[2].Value.ToString();
                    txtDienThoai.Text = dGV_DSGianHang.SelectedRows[i].Cells[3].Value.ToString();
                    txtEmail.Text = dGV_DSGianHang.SelectedRows[i].Cells[4].Value.ToString();

                }
            }
        }

        private void FormQLGianHang_Load(object sender, EventArgs e)
        {
            LayDSGH();
           
        }
        public void LayDSGH()
        {
            dservice.Connect();
            SqlParameter[] parameters1 = new SqlParameter[1];
            parameters1[0] = new SqlParameter("@username", Public_variable.Username);
            dt = dservice.ExcuteProcedureGetTable("sp_getGH_User", parameters1, null);
            dservice.Disconnect();
            if (dt.Rows.Count > 0)
            {
                dGV_DSGianHang.DataSource = dt;
                dGV_DSGianHang.Columns[0].Width = 100;
                dGV_DSGianHang.Columns[1].Width = 200;
                dGV_DSGianHang.Columns[2].Width = 250;
                dGV_DSGianHang.Columns[3].Width = 185;
            }

        }

        private void btnDKGH_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTenGH.Text != "" && txtDiaChiGH.Text != "" && txtDienThoai.Text != "" && txtEmail.Text != "" )
                {
                    dservice.Connect();

                    SqlParameter[] parameters = new SqlParameter[5];
                    parameters[0] = new SqlParameter("@tengh", txtTenGH.Text);
                    parameters[1] = new SqlParameter("@diachi", txtDiaChiGH.Text);
                    parameters[2] = new SqlParameter("@username", Public_variable.Username);
                    parameters[3] = new SqlParameter("@dienthoai", txtDienThoai.Text);
                    parameters[4] = new SqlParameter("@email", txtEmail.Text);

                    dservice.ExcuteProcedure("sp_insertGH", parameters, null);
                    MessageBox.Show("Đăng ký gian hàng thành công!!!");
                    LayDSGH();
                    txtTenGH.Text = "";
                    txtDiaChiGH.Text = "";
                    txtDienThoai.Text = "";
                    txtEmail.Text = "";
                    dservice.Disconnect();
                }
                else
                    MessageBox.Show("Thông tin chưa đầy đủ - Vui lòng kiểm tra!!!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTenGH.Text != "" && txtDiaChiGH.Text != "" && txtDienThoai.Text != "" && txtEmail.Text != "")
                {
                    dservice.Connect();

                    SqlParameter[] parameters = new SqlParameter[6];
                    parameters[0] = new SqlParameter("@tengh", txtTenGH.Text);
                    parameters[1] = new SqlParameter("@diachi", txtDiaChiGH.Text);
                    parameters[2] = new SqlParameter("@username", Public_variable.Username);
                    parameters[3] = new SqlParameter("@dienthoai", txtDienThoai.Text);
                    parameters[4] = new SqlParameter("@email", txtEmail.Text);
                    parameters[5] = new SqlParameter("@magh", maGH_select);

                    dservice.ExcuteProcedure("sp_updateGH", parameters, null);
                    MessageBox.Show("Cập nhật gian hàng thành công!!!");
                    dservice.Disconnect();
                    LayDSGH();
                    txtTenGH.Text = "";
                    txtDiaChiGH.Text = "";
                    txtDienThoai.Text = "";
                    txtEmail.Text = "";
                    
                }
                else
                    MessageBox.Show("Thông tin chưa đầy đủ - Vui lòng kiểm tra!!!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
